package com.ntlg.springboot02redis.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ntlg.springboot02redis.pojo.User;
import com.ntlg.springboot02redis.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.Writer;
import java.util.HashMap;
import java.util.List;

@Api(tags = "缓存实验")
@Controller
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/cache")
    @ResponseBody
    @ApiOperation(value = "用于测试缓存")
    public String userCache(){
        List<HashMap> all = userService.getAll();
        String s = JSON.toJSONString(all);
        redisTemplate.opsForValue().set("name",s);
        System.out.println("从缓存中拿数据");
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println(name);
        return "success";
    }
}
